import { _decorator, Camera, Color, Component, director, instantiate, Line, math, MeshRenderer, Node, Prefab, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
import ResManager from './ResManager';
import { CameraManager } from './CameraManager';

export default class WorldManager extends Component {
    private static _instance:WorldManager;

    static get instance(): WorldManager{
        if(this._instance == null){
            this._instance = new WorldManager();
        }

        return this._instance
    }
    //存储所有的地板
    private allFloor:Map<string, Node> = new Map<string, Node>()
    //从中心到边界的步数
    private radiusStep:number = 0
    private radiusRight:number = 0
    private radiusLeft:number = 0
    private radiusTop:number = 0
    private radiusBottom:number = 0
    private step:number = 0
    private size:number = 0

   
    public pickerNode:Node;
   
    public async createFloor(posX:number = 0, posZ:number = 0, stepX:number = 10,stepY:number = 10, size:number = 10){
        let plane = await ResManager.instance.loadAssetByUrl<Prefab>('prefab/plane')
        let scene = director.getScene(); 
        
      

        for(let x = posX ; x < posX + stepX; x++){
            
            for( let z=posZ ; z < posZ + stepY; z++){
                let name = x + ':' + z;
              
                // 判断该坐标位置，是否已经创建
                if(this.allFloor.has(name)){
                    continue
                }
              
                let node = instantiate(plane);
                if(x % 2 == 0 ){
                    if(z % 2 == 0){
                        node.getComponent(MeshRenderer).materials[0].setProperty('mainColor', Color.GRAY)
                    }
                    
                } else {
                    if(Math.abs(z % 2) == 1){
                        node.getComponent(MeshRenderer).materials[0].setProperty('mainColor', Color.GRAY)
                    }
                }
                

                node.name = name
                node.worldPosition = v3(x*size,0, z*size)
                node.parent = scene

                this.allFloor.set(node.name, node)
            }
        }

       
    }

    public initWorld(x:number = 0, y:number = 0, step:number = 10, size:number = 10){
        this.radiusLeft = step
        this.radiusRight = step
        this.radiusTop = step
        this.radiusBottom = step
        this.step = step
        this.size = size

        // 创建东南方
        this.createFloor(x, y, step,step, size)
        //创建东北方
        this.createFloor(x, y-step, step,step, size)
        //创建西北方
        this.createFloor(x-step, y-step,step, step, size)
        //创建西南方
        this.createFloor(x-step, y, step,step, size)

        //将相机放在原点上方的位置
        let temp = v3(0, 0, 0)
        Vec3.add(temp, v3(x*size, 0, y*size), v3(30,30,30))
        CameraManager.instance.camera.node.worldPosition = temp
    }

    public async createBorder(leftDown:boolean,rightDown:boolean,  rightUp:boolean, leftUp:boolean){
      
       
        if(!leftDown){
            // 下方（下边）
            if(this.radiusBottom < 40){
                await this.createFloor(-this.radiusBottom-this.step, this.radiusBottom, this.radiusBottom * 2+ this.step * 2, this.step)
                this.radiusBottom += this.step
            }
           
        }

        if(!rightDown){
             // 右上方  往下(右边)
             if(this.radiusRight < 40){
                await this.createFloor(this.radiusRight, -this.radiusRight-this.step, this.step, this.radiusRight *2 + this.step * 2)
                this.radiusRight += this.step
             }
           
        }

        if(!rightUp){
             // 中上方（上边）
             if(this.radiusTop < 40){
                await this.createFloor(-this.radiusTop-this.step, -this.radiusTop-this.step, this.radiusTop * 2+ this.step * 2, this.step)
                this.radiusTop += this.step
             }
        }
        if(!leftUp){
             //左上方 往下（左边）
            if(this.radiusLeft < 40){
                await this.createFloor(-this.radiusLeft-this.step, -this.radiusLeft-this.step, this.step, this.radiusLeft *2 + this.step * 2)
                this.radiusLeft += this.step
            }
            
        }

       
       
       
        

      
       

    }

    
}


